/**
 * StepInt
 *
 * @author		Allen Chou
 * @version		1.1.0 (last update: Jul 6 2008)
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.utils {

	public class StepInt {
		
		private var _min:int;
		private var _max:int;
		private var _data:int;
		
		/**
		 * StepInt constructor.
		 * @param	initNumber The initial internal Number data.
		 * @param	min The lower bound for the StepNumber.
		 * @param	max The upper bound for the StepNumber.
		 */
		public function StepInt(
				min:int = 0,
				max:int = 10,
				initNumber:int = int.MIN_VALUE) {
			
			_min = min;
			_max = max;
			setValue((initNumber == int.MIN_VALUE)?(_min):(initNumber));
			
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
			
		}
		
		//altered in version 1.01 (return type changed from void to int)
		/**
		 * Increments the int data by a certain amount, and loops it from one bound if it exceeds the other.
		 * @param	interval the amount by which to increment the int data, can be a negative int
		 */
		public function step(interval:int = 1):int {
			return setValue(_data + interval);
		}
		
		//added in version 1.01 (directly sets the _data and performs boundary check)
		/**
		 * Assigns a new value to the int data.
		 * @param	value the new value to assign to the int data
		 */
		public function setValue(value:int):int {
			var length:int = _max - _min + 1;
			_data = value;
			
			if (_data > max) {
				_data = _min + ((_data - _min) % length);
			} else if (_data < min) {
				_data = _min + ((_data - _min) % length) + length;
			}
			
			return _data;
		}
		
		/**
		 * Returns the value of the int data.
		 * */
		public function getValue():int {
			return _data;
		}
		
		/**
		 * Converts the int data to String.
		 */
		public function toString():String {
			return _data.toString();
		}
		
		/**
		 * The lower bound.
		 */
		public function get min():int { return _min; }
		public function set min(value:int):void {
			_min = value;
			
			//added in version 1.01 (boundary check)
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
		}
		
		/**
		 * The upper bound.
		 */
		public function get max():int { return _max; }
		public function set max(value:int):void {
			_max = value;
			
			//added in version 1.01 (boundary check)
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
		}
		
		//add in version 1.02 (getting length)
		/**
		 * Returns the lengh between two boundaries.
		 */
		public function getLength():int {
			return _max - _min + 1;
		}
		
	}

}